package com.ymatou.iapi.livequery.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livequery.service.ListInProgressActivitiesByIdsCall;
import com.ymatou.iapi.livequery.service.ListInProgressActivitiesBySellerIdsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.database.mongo.LiveBasicInfoWapper;
import com.ymttest.database.sqlwapper.YmtLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

@TestSuite
public class Ts_ListInProgressActivitiesBySellerIds{
	private static ListInProgressActivitiesBySellerIdsCall listInProgressActivitiesBySellerIdsCall;
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据多个直播id获取直播列表");
	}

	@Before
	public void caseUp() {
		listInProgressActivitiesBySellerIdsCall=new ListInProgressActivitiesBySellerIdsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(listInProgressActivitiesBySellerIdsCall.getOpurl());
	}
	
	
	/**
	 * 报文断言
	 * @param dataStr
	 * @throws Exception
	 */
	public void getReturnCode(JSONObject result) throws Exception {
	    try {	    	
			//返回报文断言		
			String code =  result.getString("Code");
			Logger.verifyIsContains(200, code, "\"Code\":200");
			String bCode =  result.getString("BCode");
			Logger.verifyIsContains(0, bCode, "\"BCode\":200");
			String msg =  result.getString("Msg");
			Logger.verifyIsContains("操作成功", msg, "\"Msg\":操作成功");	
	    	
	    }catch (Exception e) {  
	        e.printStackTrace();
	    }  
	}
	
	/**
	 * sql断言映射关系
	 * @return
	 * @throws Exception
	 */
	public Map<String,String> sqlMap() throws Exception {
	    try {	    	
	    	Map<String,String> sqlMap = new HashMap<String,String>();			
			sqlMap.put("sTitle","Data->Activities[0]->Title");	
			sqlMap.put("sContent","Data->Activities[0]->ActivityContent");	
			sqlMap.put("sName","Data->Activities[0]->ActivityName");	//直播名称
			sqlMap.put("sPicUrl","Data->Activities[0]->ActivityPicture");	
			sqlMap.put("iCountryId","Data->Activities[0]->CountryId");
			sqlMap.put("iSellerId","Data->Activities[0]->SellerId");	
			sqlMap.put("sSellerName","Data->Activities[0]->Seller");	
			sqlMap.put("sPosition","Data->Activities[0]->ShopAddress");	//卖家地址
			sqlMap.put("iAction","Data->Activities[0]->Action");
			return sqlMap;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;	
}
	
	
	/**
	 * 映射老Mango库关系
	 * @return
	 * @throws Exception
	 */
	public Map<String,String> oldMangoMap() throws Exception {
	    try {	    	
	    	Map<String,String> mangoMap = new HashMap<String,String>();			
	    	mangoMap.put("Title","Data->Activities[0]->Title");	
	    	mangoMap.put("ActivityContent","Data->Activities[0]->ActivityContent");	
	    	mangoMap.put("ActivityName","Data->Activities[0]->ActivityName");	//直播名称
	    	mangoMap.put("ActivityInfo","Data->Activities[0]->ActivityInfo");	//直播名称
	    	mangoMap.put("SellerId","Data->Activities[0]->SellerId");	
	    	mangoMap.put("Seller","Data->Activities[0]->Seller");	
	    	mangoMap.put("SellerLogo","Data->Activities[0]->SellerLogo");	 //卖家头像
	    	mangoMap.put("ShopAddress","Data->Activities[0]->ShopAddress");	
			mangoMap.put("ActivityPicture","Data->Activities[0]->ActivityPicture");	
			mangoMap.put("Action","Data->Activities[0]->Action");	
			mangoMap.put("Country","Data->Activities[0]->Country");
			return mangoMap;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;	
}
	
	
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListInProgressActivitiesByIds_001() {
		Logger.start(true,"根据多个直播id获取直播列表（未开始的直播无商品）");
		try {
			//TODO 补全case代码
			//新建一个未开始的直播
			Integer activityId = LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			
			List<Integer> sellerIdList=new ArrayList<>();
			sellerIdList.add(sellerId);
			
			Map<String, Object> updateMap = new HashMap<String, Object>();
			updateMap.put("SellerIds", new JSONArray(sellerIdList));
			
			JSONObject result =  listInProgressActivitiesBySellerIdsCall.callAndGetReturnData(updateMap);
			
			//报文断言
			getReturnCode(result);
			//无直播商品部分的断言
			checkListInProgressActivitiesByIds(result, activityId);						
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	

	
	/**
	 * 
	 * @param result 返回报文
	 * @param activityId 直播id
	 * @throws Exception
	 */
	public void checkListInProgressActivitiesByIds(JSONObject result,int activityId) throws Exception {
	    try {	    		
			//取出时间
			String sStartTime = result.getJSONObject("Data").getJSONArray("Activities").getJSONObject(0).getString("StartTime");
			String sEndTime = result.getJSONObject("Data").getJSONArray("Activities").getJSONObject(0).getString("EndTime");
			String sAddTime = result.getJSONObject("Data").getJSONArray("Activities").getJSONObject(0).getString("AddTime");				
			
			//报文断言 
			getReturnCode(result);
			
			//AppProductRelease库  Ymt_Activity
			List<Map> productsInActivity =  (List<Map>) new YmtLiveWapper().selectforMapByLiveId(activityId);
			HashMap<String, Object> actRetMapbysql = (HashMap<String, Object>) productsInActivity.get(0); 
			
			//获取sql映射关系
			Map<String,String> sqlMap = sqlMap();
			//SQL断言
			AssertService.assertResultEqual(sqlMap, actRetMapbysql,result);
			Logger.verifyEquals((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(actRetMapbysql.get("dBeginTime")), sStartTime, "SQL开始时间对比"); 
			Logger.verifyEquals((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(actRetMapbysql.get("dEndTime")), sEndTime, "SQL结束时间对比"); 
			Logger.verifyEquals((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(actRetMapbysql.get("dAddTime")), sAddTime, "SQL创建时间对比");
			
			
			
			//Mongo老库映射关系
			Map<String,String> oldMongoMap = oldMangoMap();
			//old Mango
			Map<String,Object> actRetMapbyoldMango =(Map<String, Object>) new LiveBasicInfoWapper().getActivityId(activityId);
			AssertService.assertResultEqual(oldMongoMap, actRetMapbyoldMango,result);
			Logger.verifyEquals(YMTDateUtil.getObjectFormatTime(actRetMapbyoldMango.get("StartTime"), "yyyy-MM-dd HH:mm:ss"),sStartTime, "Mango开始时间对比");
			Logger.verifyEquals(YMTDateUtil.getObjectFormatTime(actRetMapbyoldMango.get("EndTime"), "yyyy-MM-dd HH:mm:ss"),sEndTime, "Mango结束时间对比");
			Logger.verifyEquals(YMTDateUtil.getObjectFormatTime(actRetMapbyoldMango.get("AddTime"), "yyyy-MM-dd HH:mm:ss"),sAddTime, "Mango创建时间对比");
	    	
	    }catch (Exception e) {  
	        e.printStackTrace();
	    }  
	}
	
	
	
	
	
} 